<?php

class Portfolio extends Controller {

	function Portfolio()
	{
		parent::Controller();	
        if(!admin_session_check()) redirect('admin/login');
        if(!is_allowed("all")) show_404();
        $this->load->model('portfoliomodel');  
        $this->load->library("form_validation");       
	}     
    
    function index()
    {
		            
        $data["list"] = $this->portfoliomodel->admin_get_list();
        $data["response"] = get_response(); 

        $this->load->view('admin/default/head');  
        $this->load->view('admin/default/header', array('selected'=>'pages'));  
        $this->load->view("admin/default/nav_pages", array('selected' => 'portfolio'));
        $this->load->view("admin/default/quick_search");
        $this->load->view('admin/portfolio/portfolio_list', $data);   
        $this->load->view('admin/default/footer');   

    }
 
    function edit($id = NULL)
    {
        if($id == NULL) show_404();

        $data["item"] = $this->portfoliomodel->admin_get_item($id); 
        if(empty($data["item"])) show_404();

        $this->_process_form();
                              
        if($this->input->post("submit_action") !== FALSE){
            $data["products"] = ($this->input->post("products")) ? $this->input->post("products") : array();
            $data["images"] = ($this->input->post("images")) ? $this->input->post("images") : array();
        } else {
            $data["products"] = $this->portfoliomodel->admin_get_products($id);
            $data["images"] = $this->portfoliomodel->get_images($id);
        }

        $this->load->model('productsmodel');
        $data["available_products"] = $this->productsmodel->admin_get_all(); 

        $data["action"] = "update";
        $data["token"] = generate_token("portfolio",720);

        $this->load->view('admin/default/head');  
        $this->load->view('admin/default/header', array('selected'=>'pages'));  
        $this->load->view("admin/default/nav_pages", array('selected' => 'portfolio'));
        $this->load->view("admin/default/quick_search");
        $this->load->view('admin/portfolio/portfolio_form', $data);   
        $this->load->view('admin/default/footer');   

    }
       
    function add()
    {
        $this->_process_form();

        $data["item"] = array(
                "id_Portfolio" => "", 
                "Name_Portfolio" => "", 
                "Introduction_Portfolio" => "",
                "Description_Portfolio" => "",
                "Main_Image" => ""
        ); 
        $data["products"] = array();
        $data["images"] = array();
              
        if($this->input->post("submit_action") !== FALSE){
            if ($this->input->post("products")) $data["products"] = $this->input->post("products");
            if ($this->input->post("images")) $data["images"] = $this->input->post("images");
        }

        $this->load->model('productsmodel');
        $data["available_products"] = $this->productsmodel->admin_get_all(); 

        $data["action"] = "create";
        $data["token"] = generate_token("portfolio",720);

        $this->load->view('admin/default/head');  
        $this->load->view('admin/default/header', array('selected'=>'pages'));  
        $this->load->view("admin/default/nav_pages", array('selected' => 'portfolio'));
        $this->load->view("admin/default/quick_search");
        $this->load->view('admin/portfolio/portfolio_form', $data);   
        $this->load->view('admin/default/footer');   

    }            
    function remove($id = false){

        if($id == NULL) show_404();  

        $data["item"] = $this->portfoliomodel->admin_get_item($id);

        if(empty($data["item"])){

            set_response('error', "The installation you're attempting to delete does not exist");
            redirect('admin/portfolio');

        } else {

            $this->load->view('admin/default/head');          
            $this->load->view('admin/default/header', array('selected'=>'pages'));  
            $this->load->view("admin/default/nav_pages", array('selected' => 'portfolio'));
            $this->load->view('admin/portfolio/portfolio_delete', $data);   
            $this->load->view('admin/default/footer');   

        }
    }  
    function preview($id = NULL)
    {

        if($this->input->post("submit_action") === FALSE){

            $data["item"] = $this->portfoliomodel->admin_get_item($id); 
            if (empty($data["item"])) show_404();             

            $data["images"] = $this->portfoliomodel->get_images($id);                                 
            $data["products"] = $this->portfoliomodel->get_products($id); 

            $data["action"] = "view";

        } else {

            $data["item"] = array(
                'id_Portfolio' => $this->input->post("id", TRUE),  
                'Name_Portfolio' => $this->input->post("name", TRUE),  
                'Introduction_Portfolio' => $this->input->post("introduction", TRUE),
                'Description_Portfolio' => $this->input->post("description", TRUE),
                'Main_Image' => $this->input->post("main_image", TRUE));
            $data["images"] = ($this->input->post("images")) ? $this->input->post("images") : array();             
            $input_products = ($this->input->post("products", TRUE)) ? $this->input->post("products", TRUE) : array();
            $data["products"] = $this->portfoliomodel->get_products_data($input_products);

            $data["action"] = "preview";

        }

        //link glossary items     
        $this->load->model('glossarymodel'); 

        $this->load->view('admin/previews/preview_portfolio', $data);   

    }
    function confirm_removal($id = false){

        if($id == NULL) show_404();  

        $data["item"] = $this->portfoliomodel->admin_get_item($id);

        if(empty($data["item"])){

            set_response('error', "The installation you're attempting to delete does not exist");
            redirect('admin/portfolio');

        } else {

            $this->portfoliomodel->admin_delete_portfolio($id);
            $this->portfoliomodel->admin_delete_images($id);
            $this->portfoliomodel->admin_delete_products($id);
            set_response('success', "The installation <i>".$data["item"]["Name_Portfolio"]."</i> was deleted.");
            redirect('admin/portfolio');
        }
    }
    function deactivate($id = false){

        if($id == NULL) show_404();  
        $data = array('Active_Portfolio' => 0);
        $this->portfoliomodel->admin_update_portfolio($id, $data);
        $item = $this->portfoliomodel->admin_get_item($id);
        set_response('success', "The installation <i>".$item["Name_Portfolio"].'</i> is now inactive. <a href="'.site_url("admin/portfolio/preview/".$item["id_Portfolio"]).'" rel="view">Click to view</a>');
        redirect('admin/portfolio');
    }
    function activate($id = false){

        if($id == NULL) show_404();  
        $data = array('Active_Portfolio' => 1);
        $this->portfoliomodel->admin_update_portfolio($id, $data);
        $item = $this->portfoliomodel->admin_get_item($id);
        set_response('success', "The installation <i>".$item["Name_Portfolio"].'</i> is now active. <a href="'.site_url("admin/portfolio/preview/".$item["id_Portfolio"]).'" rel="view">Click to view</a>');
        redirect('admin/portfolio');
    }
    function approve($id = false){

        if(!is_allowed(array(ROLE_ADMIN,ROLE_PUBLISHER))) show_404();
        if($id == NULL) show_404();  

        $data = array('Approved_Portfolio' => 1, 'Active_Portfolio' => 1);
        $this->portfoliomodel->admin_update_portfolio($id, $data);
        $item = $this->portfoliomodel->admin_get_item($id);
        set_response('success', "The installation <i>".$item["Name_Portfolio"].'</i> was approved. <a href="'.site_url("admin/portfolio/preview/".$item["id_Portfolio"]).'" rel="view">Click to view</a>');
        redirect('admin/portfolio');
    }
    function _process_form()
    {        
        if($this->input->post("submit_action") !== FALSE){

            if ($this->_validate_form()) {

                if (validate_token("portfolio",$this->input->post("token",TRUE))){

                    $data = array(
                        'Name_Portfolio' => $this->input->post("name"),  
                        'Introduction_Portfolio' => $this->input->post("introduction"),
                        'Description_Portfolio' => $this->input->post("description"),
                        'Main_Image' => $this->input->post("main_image"),
                    );

                    switch($this->input->post("submit_action")){    

                        case 'create':

                            $portfolio_id = $this->portfoliomodel->admin_insert_portfolio($data);
                            set_response('success','installation <i>'.$data['Name_Portfolio'].'</i> was created. <a href="'.site_url("admin/portfolio/preview/".$portfolio_id).'" rel="view">Click to view</a>');
							
                        break;
                        case 'update':

							$portfolio_id = $this->input->post("id");
							$this->portfoliomodel->admin_update_portfolio($portfolio_id, $data);
							set_response('success','installation <i>'.$data['Name_Portfolio'].'</i> was updated. <a href="'.site_url("admin/portfolio/preview/".$portfolio_id).'" rel="view">Click to view</a>');
							
                        break;        

                    }
										

                    #### Process images ###
                    // delete previous images
                    $this->portfoliomodel->admin_delete_images($portfolio_id);
                    //add new images
                    if ($this->input->post("images") !== FALSE) {
                        $data = array_filter($this->input->post("images", TRUE));
                        $this->portfoliomodel->admin_insert_images($portfolio_id, $data);
                    } 
                    
                    #### Process products ###
                    // delete previous images
                    $this->portfoliomodel->admin_delete_products($portfolio_id);
                    //add new products
                    if ($this->input->post("products") !== FALSE) {
                        $data = array_filter($this->input->post("products", TRUE)); 
                        $this->portfoliomodel->admin_insert_products($portfolio_id, $data);
                    } 


					redirect('admin/portfolio');

                }
            }   
        }
    }

    private function _validate_form(){
        
        $config = array(
                        array(
                         'field'   => 'name',
                         'label'   => 'Installation Title',
                         'rules'   => 'trim|required|xss_clean|max_length[250]'
                        ),
                        array(
                         'field'   => 'introduction',
                         'label'   => 'Introduction',
                         'rules'   => 'trim|required|xss_clean'
                        ),
                        array(
                         'field'   => 'description',
                         'label'   => 'Description',
                         'rules'   => 'trim|required|xss_clean'
                        ),             
                        array(
                         'field'   => 'main_image',
                         'label'   => 'Main Image',
                         'rules'   => 'trim|required|xss_clean'
                        ),
                        array(
                         'field'   => 'products',
                         'label'   => 'Products Used',
                         'rules'   => 'required|xss_clean'
                        )
                    );
        
                                                 
        $this->form_validation->set_rules($config);       
        $this->form_validation->set_error_delimiters('<span class="error">', '</span>');
        
        if ($this->form_validation->run($this) == FALSE){
            return FALSE;
        }else
            return TRUE;

    }

}

/* End of file welcome.php */
/* Location: ./system/application/controllers/welcome.php */
